package org.codeCard.util;

import com.updateNew.MyIsif;
import org.cbg.until.TraslationDemoScrollBarUI;
import org.codeCard.bean.TbSaleCardOrder;
import org.codeCard.frame.CodeCardJFrame;
import org.codeCard.model.CodeCardSellerModel;
import org.codeCard.panel.CodeCardBuyJPanel;
import org.codeCard.panel.CodeCardSaleJPanel;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

public class CodeCardUntil extends JPanel {

    public static JPanel currBtn;

    public static JPanel sellCurrBtn;

    public static void setBuyTableModel(JScrollPane jScrollPane, List<TbSaleCardOrder> salegoodsList){

        JPanel view = (JPanel) jScrollPane.getViewport().getView();
        view.removeAll();
        view.repaint();
        jScrollPane.getVerticalScrollBar().setUI(new TraslationDemoScrollBarUI(jScrollPane));
        jScrollPane.setViewportView(view);
        jScrollPane.setOpaque(false);
        jScrollPane.setBorder(null);
        jScrollPane.getViewport().setOpaque(false);
        Border js = BorderFactory.createEmptyBorder();
        jScrollPane.setBorder(js);
        jScrollPane.setVerticalScrollBarPolicy(20);
        jScrollPane.setHorizontalScrollBarPolicy(31);

        class MListener extends MouseAdapter {
            @Override
            public void mouseEntered(MouseEvent e) {


                JPanel btn = (JPanel) e.getSource();
                StringBuilder sb = new StringBuilder();
                sb.append("当前进入的按钮是：\n").append(btn.getName()).append("\n").append("正在进行演示自定义Tooltip！\n")
                        .append("请自行查看源码");

            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent e) {
                if(currBtn != null){
                    currBtn.setBackground(null);
                    currBtn.setOpaque(false);
                }

                jScrollPane.setBackground(null);
                JPanel btn = (JPanel) e.getSource();
                btn.setBackground(Color.gray);
                btn.setOpaque(true);

                currBtn = btn;

                // TODO Auto-generated method stub
                CodeCardSellerModel source = (CodeCardSellerModel)e.getSource();
                CodeCardBuyJPanel sellerXianYuJpanel = CodeCardJFrame.bjczMainJpanel.getBjczCardJpanel().getBjcz1Jpanel();

                sellerXianYuJpanel.zongjia = null;
                sellerXianYuJpanel.buyCodeCard.setText("");
                sellerXianYuJpanel.dianka = source.getDianka().getText();
                sellerXianYuJpanel.meidian = source.getDanjia().getText();

                sellerXianYuJpanel.setCustomerOrder(source.getCurrtSalegoods()); ;
            }
        }
        for (int i = 0; i < salegoodsList.size(); i++) {
            CodeCardSellerModel sellerXianYuModelJpanel = new CodeCardSellerModel(
                    salegoodsList.get(i));
            if (MyIsif.getStyle().equals("水墨UI")) {
                sellerXianYuModelJpanel.setBounds(0, i * 30, 400, 30);
            }else{
                sellerXianYuModelJpanel.setBounds(0, i * 30, 400, 30);
            }
            sellerXianYuModelJpanel.setName("第" + i + "个新" + i + "面板");
            MListener m = new MListener();
            sellerXianYuModelJpanel.addMouseListener(m);
            view.add(sellerXianYuModelJpanel);
        }

        view.setPreferredSize(new Dimension(565, 50 * salegoodsList.size()));
        jScrollPane.updateUI();
        jScrollPane.invalidate();
        jScrollPane.validate();
        jScrollPane.repaint();
    }

    public static void TableModel(JScrollPane jScrollPane, int geshu) {
        /**
         * 表格
         */
        JPanel jPanel = new JPanel();
        jPanel.setBackground(null);
        jPanel.setOpaque(false);
        jPanel.setBorder(null);
        jPanel.setLayout(null);
        jScrollPane.getVerticalScrollBar().setUI(new TraslationDemoScrollBarUI(jScrollPane));
        jScrollPane.setViewportView(jPanel);
        jScrollPane.setOpaque(false);
        jScrollPane.setBorder(null);
        jScrollPane.getViewport().setOpaque(false);
        Border js = BorderFactory.createEmptyBorder();
        jScrollPane.setBorder(js);
        jScrollPane.setVerticalScrollBarPolicy(20);
        jScrollPane.setHorizontalScrollBarPolicy(31);
        /**
         * 设置监听
         *
         * @author Asus
         *
         */
        class MListener extends MouseAdapter {
            @Override
            public void mouseEntered(MouseEvent e) {
                JPanel btn = (JPanel) e.getSource();
                StringBuilder sb = new StringBuilder();
                sb.append("当前进入的按钮是：\n").append(btn.getName()).append("\n").append("正在进行演示自定义Tooltip！\n")
                        .append("请自行查看源码");
                btn.setBackground(Color.gray);
                btn.setOpaque(true);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub
                JPanel btn = (JPanel) e.getSource();
                btn.setBackground(null);
                btn.setOpaque(false);
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

                CodeCardSellerModel source = (CodeCardSellerModel)e.getSource();

//                //system.out.println(123);
            }
        }
        for (int i = 0; i < geshu; i++) {
            CodeCardSellerModel codeCardBuyJPanel = new CodeCardSellerModel();
            codeCardBuyJPanel.setBounds(0, i * 30, 400, 30);
            codeCardBuyJPanel.setName("第" + i + "个面板");
            MListener m = new MListener();
            codeCardBuyJPanel.addMouseListener(m);
            codeCardBuyJPanel.addMouseListener(new
             MouseListener() {

                 @Override
                 public void mouseReleased(MouseEvent e) {
                     // TODO Auto-generated method stub

                 }

                 @Override
                 public void mousePressed(MouseEvent e) {
                     // TODO Auto-generated method stub

                 }

                 @Override
                 public void mouseExited(MouseEvent e) {
                     // TODO Auto-generated method stub
    //             traslationWantBuyDahuabiModelJpanel.setBackground(null);
    //             traslationWantBuyDahuabiModelJpanel.setOpaque(false);
                 }

                 @Override
                 public void mouseEntered(MouseEvent e) {
                     // TODO Auto-generated method stub
                     codeCardBuyJPanel.setBackground(Color.gray);
                     codeCardBuyJPanel.setOpaque(true);
                 }

                 @Override
                 public void mouseClicked(MouseEvent e) {
                     // TODO Auto-generated method stub
                 }
             });

            jPanel.add(codeCardBuyJPanel);
        }
        jPanel.setPreferredSize(new Dimension(565, 30 * geshu));
        jScrollPane.updateUI();
        jScrollPane.invalidate();
        jScrollPane.validate();
        jScrollPane.repaint();
    }

    public static void setSellerTableModel(JScrollPane jScrollPane, List<TbSaleCardOrder> salegoodsList) {
        /**
         * 表格
         */
        JPanel view = (JPanel) jScrollPane.getViewport().getView();
        view.removeAll();
        view.repaint();
        jScrollPane.getVerticalScrollBar().setUI(new TraslationDemoScrollBarUI(jScrollPane));
        jScrollPane.setViewportView(view);
        jScrollPane.setOpaque(false);
        jScrollPane.setBorder(null);
        jScrollPane.getViewport().setOpaque(false);
        Border js = BorderFactory.createEmptyBorder();
        jScrollPane.setBorder(js);
        jScrollPane.setVerticalScrollBarPolicy(20);
        jScrollPane.setHorizontalScrollBarPolicy(31);
        /**
         * 设置监听
         *
         * @author Asus
         *
         */
        class MListener extends MouseAdapter {
            @Override
            public void mouseEntered(MouseEvent e) {
                JPanel btn = (JPanel) e.getSource();
                StringBuilder sb = new StringBuilder();
                sb.append("当前进入的按钮是：\n").append(btn.getName()).append("\n").append("正在进行演示自定义Tooltip！\n")
                        .append("请自行查看源码");
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }

            @Override
            public void mouseClicked(MouseEvent e) {

                if(sellCurrBtn != null){
                    sellCurrBtn.setBackground(null);
                    sellCurrBtn.setOpaque(false);
                }

                jScrollPane.setBackground(null);
                JPanel btn = (JPanel) e.getSource();
                btn.setBackground(Color.gray);
                btn.setOpaque(true);

                sellCurrBtn = btn;

                CodeCardSellerModel sellerXianYuModelJpanel = (CodeCardSellerModel)e.getSource();
                CodeCardSaleJPanel sellerXianYuJpanel = CodeCardJFrame.bjczMainJpanel.
                        getBjczCardJpanel().getBjcz2Jpanel();
                sellerXianYuJpanel.setCustomerOrder(sellerXianYuModelJpanel.getCurrtSalegoods()); ;
            }
        }
        for (int i = 0; i < salegoodsList.size(); i++) {
            CodeCardSellerModel sellerXianYuModelJpanel = new CodeCardSellerModel(
                    salegoodsList.get(i));
            sellerXianYuModelJpanel.setBounds(0, i * 30, 400, 30);
            sellerXianYuModelJpanel.setName("第" + i + "个新" + i + "面板");
            MListener m = new MListener();
            sellerXianYuModelJpanel.addMouseListener(m);
            view.add(sellerXianYuModelJpanel);
        }

        view.setPreferredSize(new Dimension(565, 50 * salegoodsList.size()));
        jScrollPane.updateUI();
        jScrollPane.invalidate();
        jScrollPane.validate();
        jScrollPane.repaint();
    }
}
